﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using Newtonsoft.Json;
using Telerik.Web.UI;

namespace RN.ERP.Web.EnterpriseChange
{
    public partial class ChangeDetailInfo : System.Web.UI.Page
    {
        public string CorpUserGuid 
        {
            get
            {
                if (string.IsNullOrEmpty(Request["corpUserGuid"]))
                {
                    SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
                    return user.CorpUserGuid;
                }
                return Request["corpUserGuid"];

            }
        }
        public string ObjectName 
        {
            get
            {
                return Request["objectName"];
            }
        }
        public string ObjectId
        {
            get
            {
                return Request["objectId"];
            }
        }
        public string SetId 
        { 
            get
            {
                return Request["setId"];
            }
        }
        public string VerifyFunType
        {
            get
            {
                return Request["verifyFunType"];
            }
        }
        public string FunctionType {

            get
            {
                return Request["functionType"];
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(CorpUserGuid))
                {
                    BindData();
                }
            }
        }



        private void BindData()
        {
            var corp = GetCompanyinfo(CorpUserGuid);
            if (corp != null)
            {
                ltrCorpName.Text = corp.CorpName;
                hdnCorpUserGuid.Value = CorpUserGuid;
                ucEnterpriseChange.ObjectId = SetId;
                ucEnterpriseChange.ObjectName = ObjectName;
                int funcType = 3;
                int.TryParse(FunctionType,out funcType);
                ucEnterpriseChange.FunctionType = (FunctionTypeEnum)funcType;
                ucEnterpriseChange.VerifyFunType = VerifyFunType;
            }
        }

        private Tb_Corp_CompanyInfo GetCompanyinfo(string corpUserGuid)
        {
            QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(corpUserGuid), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), user.CurrentArea));
            return corpInfo;
        }
    }
}